package com.server.Service;

import com.server.Example.UsersExample;
import com.server.entity.Users;
import com.server.mapper.UsersMapper;
import org.apache.ibatis.session.SqlSession;
import untils.MybatisClass;
import untils.Utils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Random;

/**
 * The type User service.
 *
 * @author user
 */
public class UserService {
    /**
     * Get all types list.
     *
     * @return the list
     */
    public static List<Users> getAllTypes(){
        SqlSession sqlSession = MybatisClass.getSqlSession();
        UsersMapper typesMapper = sqlSession.getMapper(UsersMapper.class);
        UsersExample usersExample = new UsersExample();
        usersExample.createCriteria().andUserActiveEqualTo(true);
        List<Users> newList= typesMapper.selectByExample(usersExample);
        sqlSession.close();
        return newList;
    }

    /**
     * Select by primary key users.
     *
     * @param id the id
     * @return the users
     */
    public static Users selectByPrimaryKey(Integer id){
        SqlSession sqlSession = MybatisClass.getSqlSession();
        UsersMapper typesMapper = sqlSession.getMapper(UsersMapper.class);
        Users types=typesMapper.selectByPrimaryKey(id);
        sqlSession.close();
        return types == null ?new Users():types;
    }

    /**
     * Instr vales int.
     *
     * @param number    the number
     * @param startTime the start time
     * @param entire    the entire
     * @return the int
     */
    public static int instrVales(int number, String startTime, String entire){
        int sumCount = 0;
        SqlSession sqlSession=MybatisClass.getSqlSession();
        UsersMapper usersMapper = sqlSession.getMapper(UsersMapper.class);
        for (int i = 0; i < number; i++) {
            String sex = String.valueOf(Utils.Sex.values()[new Random().nextInt(Utils.Sex.values().length)]);
            sumCount+=usersMapper.insert(new Users(
                    Utils.getRandomString(12)+i,
                    Utils.getRandomString(12)+i,
                    Utils.getRandomString(12)+i,
                    sex,
                    Utils.randomDate(startTime, entire),
                    Utils.getRandomString(12)+i,
                    new Random().nextBoolean(),
                    Utils.randomDate(startTime, entire),
                    BigDecimal.valueOf(Math.random() * (100 - 10) + 10).setScale(2, RoundingMode.DOWN)
            ));
            sqlSession.commit();
        }
        sqlSession.close();
        return sumCount;
    }
}
